<?php
    class category_view_Controller extends controllerBase
    {
        public function process()
        {
            $oDatabase = new database();
            $uri = URL::getLink('category/id/'.$this->iId);
            
            // Get page number
            $iLimit = 50;
            $this->aParams['limit'] = $iLimit;
            $iPage = isset($_GET['page']) ? $_GET['page'] : 1;
            $this->aParams['page'] = $iPage;
            
            $sQuery = '
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX bus: <'.URL::getLink('id/bussiness#').'>

            SELECT COUNT(*)  as ?total 
            WHERE
            {
            ?uri rdf:type bus:product.
            ?uri bus:price ?price.
            '.(URL::getParam('gia') ? 'FILTER('.$aPriceConditions[URL::getParam('gia')]['condition_param'].').' : '').'
            ?uri bus:hasManufacturer ?manufacturerUri.
            OPTIONAL{?manufacturerUri bus:name ?manufacturerName.}
            '.(URL::getParam('hang') ? "FILTER(?manufacturerName = '".URL::getParam('hang')."')." : '').'
            ?uri bus:location ?location.
            '.(URL::getParam('diadiem') ? "FILTER(?location = '".URL::getParam('diadiem')."')." : '').'
            ?uri bus:childCategoryOf ?category.
            FILTER(?category ="'.$uri.'").
            }';
            
            $result = @$oDatabase->loadQuery($sQuery);
            $iTotal = (int)$result['result']['rows'][0]['total'];
            $this->aParams['total'] = $iTotal;
            // Get number page to show
            $iNumberPages = $iTotal / $iLimit;
            $this->aParams['numberPage'] = $iNumberPages;

            // Lay danh sach cac dieu kien ve gia
            $sQuery = "SELECT * FROM condition_filter WHERE type='price' ORDER BY stt ASC";
            $aPriceConditions = $oDatabase->getListObject($sQuery,true,true);
            $this->aParams['priceCondition'] = $aPriceConditions;

            // Lay danh sach cac tinh
            $sQuery = "SELECT * FROM location order by name ASC";
            $aLocations = $oDatabase->getListObject($sQuery,false,true);
            $this->aParams['locations'] = $aLocations;

            // Lay danh sach cac nha san xuat
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT *  
            WHERE {
            ?uri rdf:type bus:manufacturer.
            ?uri bus:name ?name.
            }";

            $aResults = $oDatabase->loadQuery($sQuery);
            if(!empty($aResults['result']['rows']))
            {
                $this->aParams['aManufacturers'] = $aResults['result']['rows'];
            }

            $flagerrorCata = 1;
            $flagerrorProduct = 1;         

            $this->aParams['sIid']='Catalog';
            
            $query1 ='PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink('id/bussiness#').'>

            SELECT * 
            WHERE
            {
            ?uri rdf:type bus:category.
            ?uri bus:name ?namebus.
            OPTIONAL{?uri bus:defaultProductLayout ?defaultProductLayout.}
            ?uri bus:childCategoryOf ?categoryname.
            ?categoryname bus:name ?name.
            Filter (?uri ="'.$uri.'").
            }';
            $aResult = $oDatabase->loadQuery($query1);   
            $aCategorys=$aResult['result']['rows'];
            $aNameCatelogys= array();
            if(count($aCategorys) != 0)
            {
                $this->aParams['aCatelogysname'] = $aCategorys;
                $this->aParams['name'] = $aCategorys[0]['namebus'];
                $this->aParams['uri'] = $aCategorys[0]['uri'];
                $this->aParams['error'] = '';
                if(isset($aCategorys[0]['defaultProductLayout']))
                {
                    $this->aParams['layout'] = $aCategorys[0]['defaultProductLayout'];
                }
            }
            else
            {
                $this->aParams['isHome'] ='javascript::void();';
                $query1_1 ='PREFIX foaf: <http://xmlns.com/foaf/0.1/>
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX dc: <http://purl.org/dc/elements/1.1/>
                PREFIX bus: <'.URL::getLink('id/bussiness#').'>

                SELECT * 
                WHERE
                {
                ?uri rdf:type bus:category.
                ?uri bus:name ?namebus.
                OPTIONAL{?uri bus:defaultProductLayout ?defaultProductLayout.}
                Filter (?uri ="'.$uri.'").
                }';
                $aResult = $oDatabase->loadQuery($query1_1);   
                $aCategorys=$aResult['result']['rows'];
                $aNameCatelogys= array();
                array_push($aNameCatelogys,'Home'); 
                foreach($aCategorys as $cata)
                {
                    $this->aParams['name'] = $cata['namebus'];  
                }
                $this->aParams['aCatelogysname'] = $aNameCatelogys;
                if(isset($aCategorys[0]['defaultProductLayout']))
                {
                    $this->aParams['layout'] = $aCategorys[0]['defaultProductLayout'];
                }
            }

            $querylistProduct = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink('id/bussiness#').'>

            SELECT DISTINCT * 
            WHERE
            {
            ?uri rdf:type bus:product.
            ?uri bus:name ?name.
            ?uri bus:publishTime ?time.
            OPTIONAL {?uri bus:ownUser ?user.
            ?user foaf:familyName ?ownUser.}.
            ?uri bus:defaultImage ?image.
            OPTIONAL{?uri bus:isHightLight ?isHightLight.}
            ?uri bus:price ?price.
            '.(URL::getParam('gia') ? 'FILTER('.$aPriceConditions[URL::getParam('gia')]['condition_param'].').' : '').'
            ?uri bus:description ?description.
            ?uri bus:hasManufacturer ?manufacturerUri.
            OPTIONAL{?manufacturerUri bus:name ?manufacturerName.}
            '.(URL::getParam('hang') ? "FILTER(?manufacturerName = '".URL::getParam('hang')."')." : '').'
            ?uri bus:location ?location.
            '.(URL::getParam('diadiem') ? "FILTER(?location = '".URL::getParam('diadiem')."')." : '').'
            ?uri bus:childCategoryOf ?category.
            FILTER(?category ="'.$uri.'").
            }
            ORDER BY DESC(?time)
            LIMIT '.$iLimit.'  
            OFFSET '.(($iPage - 1) * $iLimit).'  
            ';

            $aListProducts = $oDatabase->loadQuery($querylistProduct);
            $aproducts = array();
            if(count($aListProducts['result']['rows'])!=0)
            {   
                $aproducts=$aListProducts['result']['rows'];
            }

            // Get all provider and Get data from all Provider
            $oDatabase = new database();
            $sProviderQuery = "SELECT * FROM producers";
            $aListProvider = $oDatabase->getListObject($sProviderQuery,true);
            foreach($aListProvider as $key => $aProvider)
            {
                $result = $oDatabase->loadRemoteQuery($aProvider->producer_endpoint,$querylistProduct);
                $aproducts = array_merge($aproducts,$result);
            }

            $this->aParams['alistproducts'] = $aproducts;
        }
    }
?>
